home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvibit / dvibit.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  44KB  |  1,294 lines

  1. /*
  2. **
  3. **  dvibit   1 December 1983   Mark Senn
  4. **
  5. **        21 January  1984   James L. Schaad
  6. **
  7. **  This program is a PRELIMINARY VERSION.  Improvements will be made
  8. **  before the production version is released.
  9. **
  10. **  This program displays a DVI file on a version 3.10 BBN BitGraph
  11. **  terminal.  To use the program say `dvibit dvifile'.  This program
  12. **  can only process DVI format 2 files.
  13. **
  14. **  After each page of the output is displayed the program will wait for
  15. **  the RETURN key to be typed before displaying the following page.
  16. **
  17. **  This is a `bare bones' DVI-to-BitGraph program.  Minimal error checking
  18. **  is done.  For more complete error checking dvitype(1) should be run.
  19. **
  20. **  Stephan Bechtolsheim, Bob Brown, Richard Furuta, and Robert Wells
  21. **  provided invaluable help and advice.
  22. **
  23. **
  24. **  In the new version of the program the following changes have been made
  25. **   (release date of new version 21 January 1984):
  26. **
  27. **  1.  The program no longer writes out a log file unless it is necessary
  28. **    to record non-fatal errors.  If it is necessary to make a log file
  29. **    and the directory will not allow the create of the log file then
  30. **    the program will continue as if no error had happened.  Log file
  31. **    creation may be prevented entirely by including -l in the command
  32. **    line.
  33. **
  34. **  2.  The program now defaults to going to the end of the file and reading
  35. **    in all of the font definitions before any text is displayed on the
  36. **    screen.  This means that font definitions that are encountered in
  37. **    the file are now ignored.  Note that one may return to reading in
  38. **    the font definitions as they are encountered, but this is not 
  39. **    recommended since there are no traps to prevent multiple reading in
  40. **    of font definitions.  The in line reading of font definitions may
  41. **    be obtained by including a -p in the command line.
  42. **
  43. **  3.  The program under went a major restructuring of where routines lie,
  44. **    routines are now called as subroutines when ever practical rather
  45. **    than using goto's.  This leads to a much cleaner mainline program
  46. **    and does not loose a significant amount of time to overhead since
  47. **    the program is usually I/O bound.
  48. **
  49. **  4.  The ability to skip pages in the file was added.  One may jump
  50. **    a number of pages forwards or backward in the file.  This number
  51. **    of pages was relative, no need was seen for absolute jumping since
  52. **    there may be multiple pages in the file with the same page number
  53. **    and this would result in a confict as to which page was really meant.
  54. **    This feature may be obtained from the command line by including a
  55. **    +xxx before the file name, this will skip xxx pages into the file
  56. **    before displaying any printed material.
  57. **
  58. **  5.  This also has a fairly simplistic clipping routine incorpated into
  59. **    the display of charactors.  It does not always seem to work at the
  60. **    peak but I don't want to bother trying to improve it at this time.
  61. */
  62.  
  63.  
  64. /**********************************************************************/
  65. /***********************  external definitions  ***********************/
  66. /**********************************************************************/
  67.  
  68. #include "commands.h"
  69. #include <sgtty.h>
  70. #include <signal.h>
  71. #include <stdio.h>
  72.  
  73. char *getenv();
  74. char *index();
  75. char *malloc();
  76. char *rindex();
  77. char *sprintf();
  78. char *strcpy();
  79.  
  80. /* #define Debug    /* when testing */
  81.  
  82. /* We leave USEGLOBALMAG undefined because there are only a very limited number
  83.    font magnifications provided for this program and if it is defined, any
  84.    dvi file is as likely as not to fail */
  85. /* ---fixme--- put something into the log file if it's not mag 1000 */
  86. /* #define  USEGLOBALMAG    /* when defined, the dvi global magnification is applied */
  87.  
  88. #define  BIGBGCHAR       32  /* number of 16 bit words in a big character */
  89. #define  DVIFORMAT        2
  90. #define  FALSE            0
  91. #define  FIRSTBGCHAR     32
  92. #define  FIRSTPXLCHAR     0
  93. #ifndef FONTAREA
  94. #define  FONTAREA         "/usr/lib/tex/fonts/"
  95. #endif
  96. #define  LASTBGCHAR     127
  97. #define  LASTPXLCHAR    127
  98. #define  NBGFONTS         3  /* number of PXL fonts that can be downloaded into the BitGraph */
  99. #define  NPXLCHARS      128
  100. #define  PXLID         1001
  101. #define  RESOLUTION     118  /* is really 91.72192515 */
  102. #define  STACKSIZE      100
  103. #define  STRSIZE        257
  104. #define  TRUE             1
  105. #define  XDIFFMAX        20
  106. #define  XDIFFORG         (FIRSTBGCHAR+XDIFFMAX)
  107. #define  XSIZE          768
  108. #define  YDIFFMAX        20
  109. #define  YDIFFORG         (FIRSTBGCHAR+XDIFFMAX+1+XDIFFMAX+YDIFFMAX)
  110. #define  YSIZE         1024
  111.  
  112. int G_errenc = FALSE;      /* has an error been encountered?          */
  113. char G_Logname[STRSIZE];    /* name of log file, if created            */
  114. int G_interactive = TRUE;  /* is the program running interactively    */
  115.                            /* (i.e., standard output not redirected)? */
  116. int G_logging = 0;         /* Is a log file being created?            */
  117. struct sgttyb G_intty;     /* information about stdin if interactive  */
  118. FILE *G_logfp;             /* log file pointer (for errors)           */
  119. char G_progname[STRSIZE];  /* program name                            */
  120.  
  121. struct char_entry {  /* character entry */
  122.    unsigned short wp, hp;  /* width and height in pixels                       */
  123.    short xoffp, yoffp;     /* x offset and y offset in pixels                  */
  124.    int pfrp;               /* pixel file raster pointer                        */
  125.    int tfmw;               /* TFM width                                        */
  126.    int pxlw;               /* pixel width (the TFM width rounded to pixels)    */
  127.    unsigned istoobig : 1;  /* is the character too big to fit in the BitGraph? */
  128.    unsigned isloaded : 1;  /* is the character loaded in the BitGraph?         */
  129.                            /* We depend on the two preceding bit fields to     */
  130.                            /* be zero initially; they are not explicitly set.  */
  131.    int bgfont, bgchar;     /* BitGraph font and character                      */
  132. };
  133.  
  134.  
  135. struct font_entry {  /* font entry */
  136.    int k, c, s, d, a, l;  char n[STRSIZE];  /* FNT_DEF command parameters               */
  137.    int font_space;                          /* computed from FNT_DEF s parameter        */
  138.    int font_mag;                            /* computed from FNT_DEF s and d parameters */
  139.    char name[STRSIZE];                      /* full name of PXL file                    */
  140.    int magnification;                       /* magnification read from PXL file         */
  141.    int designsize;                          /* design size read from PXL file           */
  142.    struct char_entry ch[NPXLCHARS];         /* character information                    */
  143.    struct font_entry *next;                 /* pointer to next font entry               */
  144. };
  145.  
  146.  
  147.  
  148. /************************************************************************/
  149.  
  150.  
  151. struct char_entry *bgcp[NBGFONTS+(NBGFONTS+2)/3][NPXLCHARS];
  152.                                     /* Pointer to corresponding char_entry for   */
  153.                                     /* this BitGraph font and character.  These  */
  154.                                     /* pointers are used to set the char_entry's */
  155.                                     /* status to not loaded when we have to reuse*/
  156.                                     /* the BitGraph character.  We depend on all */
  157.                                     /* these being NULL initially;  they are not */
  158.                                     /* explicitly set.                           */
  159. float conv;                         /* converts DVI units to pixels              */
  160. int den;                            /* denominator specified in preamble         */
  161. FILE *dvifp;                        /* DVI file pointer                          */
  162. struct font_entry *fontptr;         /* font_entry pointer                        */
  163. int fullfont = 0;                   /* full font to load in BitGraph             */
  164. struct font_entry *hfontptr = NULL; /* head font_entry pointer                   */
  165. int h;                              /* current horizontal position               */
  166. int hh;                             /* current horizontal position in pixels     */
  167. int mag;                            /* magnification specified in preamble       */
  168. int num;                            /* numerator specified in preamble           */
  169. int partchar = FIRSTBGCHAR;         /* partial font character to load in BitGraph*/
  170. int partfont = NBGFONTS;            /* partial font to load in BitGraph          */
  171. int pbghpos;                        /* previous BitGraph horizontal position     */
  172. int pbgvpos;                        /* previous BitGraph vertical position       */
  173. struct font_entry *pfontptr = NULL; /* previous font_entry pointer               */
  174. int pbgf = -1;                      /* previous BItGraph font                    */
  175. long postambleptr;                  /* Pointer to the postamble                  */
  176. FILE *pxlfp;                        /* PXL file pointer                          */
  177. struct tchars tcb;                  /* information about special terminal chars  */
  178. struct sgttyb tty;                  /* to see if program is running interactively*/
  179. int xdiff;                          /* x difference                              */
  180. int xscreen;                        /* x screen adjustment                       */
  181. int v;                              /* current vertical position                 */
  182. int vv;                             /* current vertical position in pixels       */
  183. int ydiff;                          /* y difference                              */
  184. int yscreen;                        /* y screen adjustment                       */
  185.  
  186.  
  187.  
  188. /**********************************************************************/
  189. /*******************************  main  *******************************/
  190. /**********************************************************************/
  191.  
  192. main(argc, argv)
  193.    int argc;
  194.    char *argv[];
  195.  
  196. {
  197.    void GotInterrupt();
  198.    float ActualFactor();
  199.  
  200.  
  201.    struct stack_entry {  /* stack entry */
  202.       int h, hh, v, vv, w, x, y, z;  /* what's on stack */
  203.    };
  204.  
  205.    int argind;                            /* argument index for flags            */
  206.    int command;                           /* current command                     */
  207.    long cpagep;                           /* current page pointer                */
  208.    char curarea[STRSIZE];                 /* current file area                   */
  209.    char curext[STRSIZE];                  /* current file extension              */
  210.    char curname[STRSIZE];                 /* current file name                   */
  211.    char filename[STRSIZE];                /* file name                           */
  212.    register int i;                        /* command parameter; loop index       */
  213.    int k;                                 /* temperary parameter                 */
  214.    char n[STRSIZE];                       /* command parameter                   */
  215.    int PagesLeft;                         /* Number of pages left to skip        */
  216.    int PreLoad = TRUE;                    /* preload the font descriptions?      */
  217.    long ppagep;                           /* previous page pointer               */
  218.    int SkipMode = FALSE;                  /* in skip mode flag                   */
  219.    int sp;                                /* stack pointer                       */
  220.    struct stack_entry stack[STACKSIZE];   /* stack                               */
  221.    int t;                                 /* temporary                           */
  222.    char tc;                               /* temporary character                 */
  223.    char *tcp, *tcp1;                      /* temporary character pointers        */
  224.    register struct char_entry *tcharptr;  /* temporary char_entry pointer        */
  225.    int val, val2;                         /* hold temporary values for commands  */
  226.    int w;                                 /* current horizontal spacing          */
  227.    int x;                                 /* current horizontal spacing          */
  228.    int y;                                 /* current vertical spacing            */
  229.    int z;                                 /* current vertical spacing            */
  230.  
  231.    strcpy(G_progname, argv[0]);
  232.  
  233.    if (argc < 2)  {
  234.       fprintf(stderr, "\nusage: %s -pl +pages dvifile\n\n", G_progname);
  235.       exit(1);
  236.    }
  237.  
  238.    if (ioctl(1,TIOCGETP,&tty) == -1)
  239.       G_interactive = FALSE;
  240.  
  241.    if (G_interactive)  {
  242.       if ((tcp=getenv("TERM")) == NULL)  {
  243.           fprintf (stderr, "\n%s: can't find environment variable \"TERM\"\n\n", G_progname);
  244.           exit (1);
  245.       } else {
  246.          if (strcmp(tcp,"bitgraph") && strncmp(tcp,"bg",2))  {
  247.             fprintf(stderr, "\n%s: can't run interactively on non-BitGraph terminal\n\n", G_progname);
  248.             exit(1);
  249.          }
  250.       }
  251.    }
  252.  
  253.    argind = 1;
  254.    while (*argv[argind] == '-')
  255.     { tcp = argv[argind];
  256.       tcp++;
  257.       switch(*tcp){
  258.            case 'l':        /* l prohibits logging of errors */
  259.            G_logging = -1;
  260.            break;
  261.  
  262.            case 'p':        /* p prohibits pre-font loading */
  263.            PreLoad = FALSE;
  264.            break;
  265.  
  266.            default:
  267.                printf("%c is not a legal flag\n", *tcp);
  268.            }
  269.        argind++;
  270.     }
  271.  
  272.    if (*argv[argind] == '+')
  273.       {    sscanf(&argv[argind][1], "%d", &PagesLeft);
  274.           argind++;
  275.     SkipMode = TRUE;
  276.     PagesLeft++;
  277.       }
  278.  
  279.    tcp = rindex(argv[argind], '/');
  280.    if (tcp == NULL)  {
  281.       curarea[0] = '\0';
  282.       tcp = argv[argind];
  283.    }  else  {
  284.       strncpy(curarea, argv[argind], tcp-argv[argind]+1);
  285.       tcp += 1;
  286.    }
  287.    tcp1 = index(tcp, '.');
  288.    if (tcp1 == NULL)  {
  289.       strcpy(curname, tcp);
  290.       tcp1 = index(tcp, '\0');
  291.    }  else
  292.       strncpy(curname, tcp, tcp1-tcp);
  293.    strcpy(curext, tcp1);
  294.  
  295.    strcpy(filename, curarea);
  296.    strcat(filename, curname);
  297.    if (curext[0] == '\0')
  298.       strcat(filename, ".dvi");
  299.    else
  300.       strcat(filename, curext);
  301.    if ((dvifp=fopen(filename,"r")) == NULL)  {
  302.       fprintf(stderr, "\n%s: can't open DVI file \"%s\"\n\n", G_progname, filename);
  303.       exit(1);
  304.    }
  305.  
  306.    strcpy(G_Logname, curname);
  307.    strcat(G_Logname, ".loh");
  308.  
  309.    if (NoSignExtend(dvifp,1) != PRE)  {
  310.       fprintf(stderr, "\n%s: PRE doesn't occur first--are you sure this is a DVI file?\n\n", G_progname);
  311.       exit(1);
  312.    }
  313.  
  314.    signal(SIGINT, SIG_IGN);  /* ignore interrupts */
  315.    InitTerm();
  316.    signal(SIGINT, GotInterrupt);  /* catch interrupts */
  317.  
  318.    if (G_interactive)  {
  319.       ioctl(0, TIOCGETC, &tcb);  /* get special characters */
  320.       ioctl(0, TIOCGETP, &G_intty);  /* get modes */
  321.       tty = G_intty;
  322.       tty.sg_flags |= CBREAK;
  323.       tty.sg_flags &= ~ECHO;
  324.       ioctl(0, TIOCSETP, &tty);  /* set modes */
  325.    }
  326.  
  327.    i = SignExtend(dvifp,1);
  328.    if (i != DVIFORMAT)  {
  329.       fprintf(stderr, "\n%s: DVI format = %d, can only process DVI format %d files\n\n", G_progname, i, DVIFORMAT);
  330.       exit(1);
  331.    }
  332.    if (PreLoad)
  333.       { ReadPostAmble(); 
  334.     fseek(dvifp, (long) 14, 0);
  335.       } else
  336.       { num = NoSignExtend(dvifp,4);
  337.     den = NoSignExtend(dvifp,4);
  338. #ifdef USEGLOBALMAG
  339.     mag = NoSignExtend(dvifp,4);
  340.     conv = ((float)num/(float)den) * ((float)mag/1000.0) * ((float)RESOLUTION/254000.0);
  341. #else
  342.     mag = NoSignExtend(dvifp,4);        /* get it even though it'll be ignored */
  343.     conv = ((float)num/(float)den) * ((float)RESOLUTION/254000.0);
  344. #endif
  345.       }
  346.  
  347.    k = NoSignExtend(dvifp,1);
  348.    GetBytes(dvifp, n, k);
  349.  
  350.    while (TRUE)
  351.  
  352.       switch (command=NoSignExtend(dvifp,1))  {
  353.  
  354.          case SET1:  case SET2:  case SET3:  case SET4:
  355.         val = NoSignExtend(dvifp, command-SET1+1);
  356.         if (!SkipMode) prochar(val, command);
  357.             break;
  358.  
  359.          case SET_RULE:
  360.         val = NoSignExtend(dvifp, 4);
  361.         val2 = NoSignExtend(dvifp,4);
  362.             if (!SkipMode) prorule(val, val2, 1);
  363.             break;
  364.  
  365.          case PUT1:  case PUT2:  case PUT3:  case PUT4:
  366.         val = NoSignExtend(dvifp,command-PUT1+1);
  367.         if (!SkipMode) prochar(val, command);
  368.             break;
  369.  
  370.          case PUT_RULE:
  371.         val = NoSignExtend(dvifp, 4);
  372.         val2 = NoSignExtend(dvifp,4);
  373.             if (!SkipMode) prorule(val, val2, 0);
  374.             break;
  375.  
  376.          case NOP:
  377.             break;
  378.  
  379.          case BOP:
  380.             cpagep = ftell(dvifp) - 1;
  381.             for (i=0; i<=9; i++)
  382.                NoSignExtend(dvifp,4);
  383.             ppagep = NoSignExtend(dvifp,4);
  384.  
  385.             printf("\033[2J");  /* ED  Erase in Display */
  386.             pbghpos = pbgvpos = -99;
  387.             h = hh = v = vv = w = x = y = z = 0;
  388.             sp = 0;
  389.             fontptr = NULL;
  390.         if (PagesLeft)
  391.            { PagesLeft--;
  392.           if (!PagesLeft) SkipMode = FALSE;
  393.         };
  394.             break;
  395.  
  396.          case EOP:
  397.             printf("\033:-99;-99m");  /* BBNMOV  Move Graphically */
  398.             if (G_interactive && !SkipMode)  {
  399.            t = FALSE;
  400.            while (!t)  {  /* sorry about this flow of control kludge */
  401.             t = TRUE;
  402.           tc = getchar();
  403.                   if (tc==tcb.t_eofc) AllDone();
  404.                   switch (tc)  {
  405.              case '\n':
  406.             xscreen = yscreen = 0;
  407.             break;
  408.  
  409.                      case 'd':
  410.             yscreen -= (YSIZE/3);
  411.                         fseek(dvifp, cpagep, 0);
  412.                         break;
  413.  
  414.                      case 'l':
  415.             xscreen -= (XSIZE/3);
  416.                         fseek(dvifp, cpagep, 0);
  417.                         break;
  418.  
  419.              case 'p':
  420.             if (ppagep != -1)  {
  421.                fseek(dvifp, ppagep, 0);
  422.             }  else  {
  423.                fseek(dvifp, cpagep, 0);
  424.             }
  425.             xscreen = yscreen = 0;
  426.             break;
  427.  
  428.                      case 'r':
  429.             xscreen += (XSIZE/3);
  430.                         fseek(dvifp, cpagep, 0);
  431.                         break;
  432.  
  433.                      case 'u':
  434.             yscreen += (YSIZE/3);
  435.                         fseek(dvifp, cpagep, 0);
  436.                         break;
  437.  
  438.              case '-':
  439.             /* read in val */
  440.             val = ReadInt();
  441.             while (val-- && ppagep != -1)
  442.                 { fseek(dvifp, ppagep, 0);
  443.                   NoSignExtend(dvifp, 1);
  444.                   for(i=0; i<=9; i++)
  445.                     NoSignExtend(dvifp, 4);
  446.                   cpagep = ppagep;
  447.                   ppagep = NoSignExtend(dvifp, 4);
  448.                 }
  449.             fseek(dvifp, cpagep, 0);
  450.             xscreen = yscreen = 0;
  451.             break;
  452.  
  453.              case '+':
  454.             /* read in val */
  455.             val = ReadInt();
  456.             SkipMode = TRUE;
  457.             PagesLeft = val;
  458.             break;
  459.  
  460.              default:
  461.             t = FALSE;
  462.                   }
  463.                }
  464.             }
  465.             break;
  466.  
  467.          case PUSH:
  468.             if (sp >= STACKSIZE)
  469.                Fatal("stack overflow");
  470.             stack[sp].h = h;
  471.             stack[sp].hh = hh;
  472.             stack[sp].v = v;
  473.             stack[sp].vv = vv;
  474.             stack[sp].w = w;
  475.             stack[sp].x = x;
  476.             stack[sp].y = y;
  477.             stack[sp].z = z;
  478.             sp++;
  479.             break;
  480.  
  481.          case POP:
  482.             --sp;
  483.             if (sp < 0)
  484.                Fatal("stack underflow");
  485.             h = stack[sp].h;
  486.             hh = stack[sp].hh;
  487.             v = stack[sp].v;
  488.             vv = stack[sp].vv;
  489.             w = stack[sp].w;
  490.             x = stack[sp].x;
  491.             y = stack[sp].y;
  492.             z = stack[sp].z;
  493.             break;
  494.  
  495.          case RIGHT1:  case RIGHT2:  case RIGHT3:  case RIGHT4:
  496.             val = SignExtend(dvifp,command-RIGHT1+1);
  497.         if (!SkipMode) MoveOver(val);
  498.             break;
  499.  
  500.          case W0:
  501.             if (!SkipMode) MoveOver(w);
  502.             break;
  503.  
  504.          case W1:  case W2:  case W3:  case W4:
  505.             w = SignExtend(dvifp,command-W1+1);
  506.             if (!SkipMode) MoveOver(w);
  507.             break;
  508.  
  509.          case X0:
  510.             if (!SkipMode) MoveOver(x);
  511.             break;
  512.  
  513.          case X1:  case X2:  case X3:  case X4:
  514.             x = SignExtend(dvifp,command-X1+1);
  515.         if (!SkipMode) MoveOver(x);
  516.             break;
  517.  
  518.          case DOWN1:  case DOWN2:  case DOWN3:  case DOWN4:
  519.             val = SignExtend(dvifp,command-DOWN1+1);
  520.         if (!SkipMode) MoveDown(val);
  521.             break;
  522.  
  523.          case Y0:
  524.             if (!SkipMode) MoveDown(y);
  525.             break;
  526.  
  527.          case Y1:  case Y2:  case Y3:  case Y4:
  528.             y = SignExtend(dvifp,command-Y1+1);
  529.             if (!SkipMode) MoveDown(y);
  530.             break;
  531.  
  532.          case Z0:
  533.             if (!SkipMode) MoveDown(z);
  534.             break;
  535.  
  536.          case Z1:  case Z2:  case Z3:  case Z4:
  537.             z = SignExtend(dvifp,command-Z1+1);
  538.         if (!SkipMode) MoveDown(z);
  539.             break;
  540.  
  541.          case FNT1:  case FNT2:  case FNT3:  case FNT4:
  542.             if (!SkipMode) SetFntNum(NoSignExtend(dvifp,command-FNT1+1));
  543.             break;
  544.  
  545.          case XXX1:  case XXX2:  case XXX3:  case XXX4:
  546.             k = NoSignExtend(dvifp,command-XXX1+1);
  547.             while (k--)
  548.                NoSignExtend(dvifp,1);
  549.             break;
  550.  
  551.          case FNT_DEF1:  case FNT_DEF2:  case FNT_DEF3:  case FNT_DEF4:
  552.         if (PreLoad) 
  553.         { SkipFontDef (NoSignExtend(dvifp, command-FNT_DEF1+1));
  554.         } else
  555.         { ReadFontDef (NoSignExtend(dvifp, command-FNT_DEF1+1));
  556.         }
  557.             break;
  558.  
  559.          case PRE:
  560.             Fatal("PRE occurs within file");
  561.             break;
  562.  
  563.          case POST:
  564.         fseek(dvifp, cpagep, 0);
  565.         SkipMode = FALSE;
  566.         PagesLeft = 0;
  567.             break;
  568.  
  569.          case POST_POST:
  570.             Fatal("POST_POST with no preceding POST");
  571.             break;
  572.  
  573.          default:
  574.         if (command >= FNT_NUM_00 && command <= FNT_NUM_63)
  575.         {if (!SkipMode) SetFntNum(command - FNT_NUM_00);}
  576.         else if (command >= SET_CHAR_000 && command <= SET_CHAR_127)
  577.         {if (!SkipMode) prochar(command - SET_CHAR_000, command);}
  578.         else 
  579.         Fatal("%d is an undefined command", command);
  580.             break;
  581.  
  582.       }
  583.  
  584. }
  585.  
  586.  
  587.  
  588. /* */
  589.  
  590. static prochar(c, command)
  591.   int c, command;
  592. {
  593.   register struct char_entry *tcharptr;  /* temporary char_entry pointer       */
  594.     
  595.   tcharptr = &(fontptr->ch[c]);
  596.   if (tcharptr->istoobig)
  597.       { fflush(stdout);
  598.         if (fontptr != pfontptr)
  599.        { if (pfontptr != NULL)
  600.                  fclose(pxlfp);
  601.              if ((pxlfp=fopen(fontptr->name,"r")) == NULL)
  602.                  Fatal("PXL file \"%s\" disappeared (big character)! - %d", fontptr->name, fontptr);
  603.              pfontptr = fontptr;
  604.            }
  605.     printf(                                  "\033:%d;%dm",
  606.                (pbghpos=(hh-tcharptr->xoffp))+xscreen  ,
  607.            (pbgvpos=(YSIZE-vv+tcharptr->yoffp))+yscreen  );  /* BBNMOV  Move Graphically */
  608.         fseek(pxlfp, tcharptr->pfrp*4, 0);
  609.         printf("\033P:1;%d;%ds", tcharptr->wp, tcharptr->hp);  /* BBNDPD  Display Pixel Data */
  610.         LoadRast(pxlfp, tcharptr->wp, tcharptr->hp);
  611.         printf("\033\\");  /* ST  String Terminator */
  612.       } else
  613.       { if (!tcharptr->isloaded)
  614.        { fflush(stdout);
  615.              if (fontptr != pfontptr)
  616.         { if (pfontptr != NULL)
  617.                         fclose(pxlfp);
  618.                   if ((pxlfp=fopen(fontptr->name,"r")) == NULL)
  619.                         Fatal("PXL file \"%s\" disappeared (normal character)! - b", fontptr->name);
  620.                   pfontptr = fontptr;
  621.                 }
  622.              if (bgcp[tcharptr->bgfont][tcharptr->bgchar] != NULL)
  623.              if (bgcp[tcharptr->bgfont][tcharptr->bgchar]->isloaded)
  624.         { bgcp[tcharptr->bgfont][tcharptr->bgchar]->isloaded = FALSE;
  625.                   printf("\033:2;%d;%cL", tcharptr->bgchar, '0'+tcharptr->bgfont);
  626.                               /* BBNSFP  Set Font Parameters to reset character */
  627.                 }
  628.              fseek(pxlfp, tcharptr->pfrp*4, 0);
  629.              printf("\033P:0;%d;%c;%d;%d;%d;%d;%dL",
  630.                                  tcharptr->bgchar  ,    (char)('0'+tcharptr->bgfont)  ,
  631.                                      tcharptr->wp  ,                    tcharptr->hp  ,
  632.                                   tcharptr->xoffp  ,  tcharptr->hp-tcharptr->yoffp-1  ,
  633.                   tcharptr->pxlw-tcharptr->xoffp  );  /* BBNLFC  Load Font Character */
  634.              LoadRast(pxlfp, tcharptr->wp, tcharptr->hp);
  635.              printf("\033\\");  /* ST  String Terminator */
  636.              tcharptr->isloaded = TRUE;
  637.              bgcp[tcharptr->bgfont][tcharptr->bgchar] = tcharptr;
  638.           }
  639.        if ((hh - tcharptr->xoffp + tcharptr->pxlw <= XSIZE - xscreen)
  640.          && (hh /* - tcharptr->xoffp */ >= - xscreen) && (vv <= YSIZE + yscreen)
  641.          && (vv >= yscreen))
  642.       { xdiff = hh-pbghpos;  ydiff = YSIZE-vv-pbgvpos;
  643.             if ((xdiff) || (ydiff))
  644.                if ((abs(xdiff) <= XDIFFMAX) && (abs(ydiff) <= YDIFFMAX))
  645.           { putchar('\016');  /* SO  Switches to the G1 character set */
  646.                     if (xdiff)
  647.                         putchar(XDIFFORG+xdiff);
  648.                     if (ydiff)
  649.                         putchar(YDIFFORG+ydiff);
  650.                     putchar('\017');  /* SI  Switches to the G0 character set */
  651.                   } else
  652.             printf("\033:%d;%dm", hh+xscreen, YSIZE-vv+yscreen);  /* BBNMOV  Move Graphically */
  653.                if (tcharptr->bgfont != pbgf)
  654.           { printf("\033(%c", (char)('0'+tcharptr->bgfont));  /* SCS  Select G0 to be a loadable font */
  655.                     pbgf = tcharptr->bgfont;
  656.                   }
  657.                putchar(tcharptr->bgchar);
  658.                pbghpos = hh+tcharptr->pxlw-tcharptr->xoffp;  pbgvpos = YSIZE-vv;
  659.           }
  660.        }  
  661.     if (command <= SET4)
  662.     {  /* note dependency on command opcodes */
  663.           h += tcharptr->tfmw;
  664.           hh += tcharptr->pxlw;
  665.     }
  666. }
  667.  
  668.  
  669. /**********************************************************************/
  670. /*****************************  prorule  ******************************/
  671. /**********************************************************************/
  672.  
  673. /*   this routine will draw a rule on the screen */
  674.  
  675. static prorule(a, b, Set)  /* this should be a, b not b, a but it does not
  676.                 work if that is done so I swapped them jls */
  677. int a, b, Set;
  678. {
  679.    if ((a>0) && (b>0))
  680.         printf("\033:15;%d;%d;%d;%d;%d;%dr",
  681.                    hh+xscreen  ,   YSIZE-vv+yscreen  ,
  682.                    hh+xscreen  ,   YSIZE-vv+yscreen  ,
  683.                     RulePixels(b,conv)  ,  RulePixels(a,conv));  /* BBNRAST  Rastop */
  684.    if (Set)
  685.     { h += b;
  686.           hh += RulePixels(b, conv);
  687.         }
  688. }
  689.  
  690.  
  691. /**********************************************************************/
  692. /****************************  SetFntNum  *****************************/
  693. /**********************************************************************/
  694.  
  695. /*  this routine is used to specify the font to be used in printing future 
  696.     characters */
  697.  
  698. static SetFntNum(k)
  699. int k;
  700. {
  701.     fontptr = hfontptr;
  702.     while ((fontptr!=NULL) && (fontptr->k!=k))
  703.                fontptr = fontptr->next;
  704.     if (fontptr == NULL)
  705.                Fatal("font %d undefined", k);
  706. }
  707.  
  708.  
  709. /**********************************************************************/
  710. /****************************  MoveOver  ******************************/
  711. /**********************************************************************/
  712.  
  713. static MoveOver(b)
  714. int b;
  715. {
  716.    h += b;
  717.    if (abs(b) >= fontptr->font_space)
  718.           hh = PixRound(h, conv);
  719.       else
  720.           hh += PixRound(b, conv);
  721. }
  722.  
  723.  
  724. /**********************************************************************/
  725. /****************************  MoveDown  ******************************/
  726. /**********************************************************************/
  727.  
  728. static MoveDown(a)
  729. int a;
  730. {
  731.    v += a;
  732.    if (abs(a) >= 5*fontptr->font_space)
  733.          vv = PixRound(v, conv);
  734.       else
  735.          vv += PixRound(a, conv);
  736. }
  737.  
  738.  
  739. /**********************************************************************/
  740. /************************  FindPostAmblePtr  **************************/
  741. /**********************************************************************/
  742.  
  743. /* this routine will move to the end of the file and find the start
  744.     of the postamble */
  745.  
  746. static  FindPostAmblePtr (postambleptr)
  747. long    *postambleptr;
  748. {
  749.     int     i;
  750.  
  751.     fseek (dvifp, (long) 0, 2);   /* goto end of file */
  752.     *postambleptr = ftell (dvifp) - 4;
  753.     fseek (dvifp, *postambleptr, 0);
  754.  
  755.     while (TRUE) {
  756.     fseek (dvifp, --(*postambleptr), 0);
  757.     if (((i = NoSignExtend (dvifp, 1)) != 223) &&
  758.         (i != DVIFORMAT))
  759.         Fatal ("Bad end of DVI file");
  760.     if (i == DVIFORMAT)
  761.         break;
  762.     }
  763.     fseek (dvifp, (*postambleptr) - 4, 0);
  764.     (*postambleptr) = NoSignExtend (dvifp, 4);
  765.     fseek (dvifp, *postambleptr, 0);
  766. }
  767.  
  768.  
  769. /**********************************************************************/
  770. /**************************  ReadPostAmble  ***************************/
  771. /**********************************************************************/
  772.  
  773. /* this routine is used to read in the postable values.  It initializes
  774.     the magnification and checks the stack height prior to starting
  775.     printing the document  */
  776.  
  777. static  ReadPostAmble()
  778. {
  779.     FindPostAmblePtr (&postambleptr);
  780.     if (NoSignExtend (dvifp, 1) != POST)
  781.     Fatal ("POST missing at head of postamble");
  782. #ifdef Debug
  783.     fprintf (stderr, "got POST command\n");
  784. #endif
  785. /*    lastpageptr = */ NoSignExtend (dvifp, 4);
  786.     num = NoSignExtend (dvifp, 4);
  787.     den = NoSignExtend (dvifp, 4);
  788. #ifdef USEGLOBALMAG
  789.     mag = NoSignExtend(dvifp,4);
  790.     conv = ((float)num/(float)den) * ((float)mag/1000.0) * ((float)RESOLUTION/254000.0);
  791. #else
  792.    mag = NoSignExtend(dvifp,4);        /* get it even though it'll be ignored */
  793.    conv = ((float)num/(float)den) * ((float)RESOLUTION/254000.0);
  794. #endif
  795.     /* denominator/numerator here since will be dividing by the conversion
  796.        factor */
  797.     NoSignExtend(dvifp,4);        /* height-plus-depth of tallest page */
  798.     NoSignExtend(dvifp,4);              /* width of widest page */
  799.     if (NoSignExtend (dvifp, 2) >= STACKSIZE)
  800.     Fatal ("Stack size is too small");
  801.     NoSignExtend(dvifp, 2);        /* this read the number of pages in the DVI
  802.                    file */
  803. #ifdef Debug
  804.     fprintf (stderr, "now reading font defs");
  805. #endif
  806.     GetFontDef ();
  807. }
  808.  
  809.  
  810.  
  811. /**********************************************************************/
  812. /****************************  GetFontDef  ****************************/
  813. /**********************************************************************/
  814. static  GetFontDef()
  815. /* to read the font definition as they are in the postamble of the DVI file.
  816.    note that the font directory is not yet loaded I order to adapt ourselves
  817.    to the existing "verser" the following font paramters are copied onto
  818.    output fontno ( 4 bytes), chksum, fontmag, fontnamelength ( 1 byte),
  819.    fontname.  At the end a -1 is put onto the file */
  820. {
  821.     char    str[50], *calloc ();
  822.     unsigned char   byte;
  823.     int     i, fnamelen;
  824.  
  825.     while (((byte = NoSignExtend (dvifp, 1)) >= FNT_DEF1) && (byte <= FNT_DEF4))
  826.     { ReadFontDef (NoSignExtend(dvifp, byte - FNT_DEF1 + 1));
  827.     }
  828.  
  829.     if (byte != POST_POST)
  830.     Fatal ("POST_POST missing after fontdefs");
  831. }
  832.  
  833.  
  834.  
  835. /**********************************************************************/
  836. /***************************  ReadFontDef  ****************************/
  837. /**********************************************************************/
  838. static int ReadFontDef (k)
  839.   int k;
  840. {
  841.     int c, s, d, a, l, t, i;
  842.     char n[STRSIZE];
  843.     struct font_entry *tfontptr;         /* temporary font_entry pointer         */
  844.     register struct char_entry *tcharptr;  /* temporary char_entry pointer       */
  845.     
  846.     c = NoSignExtend(dvifp,4);
  847.     s = NoSignExtend(dvifp,4);
  848.     d = NoSignExtend(dvifp,4);
  849.     a = NoSignExtend(dvifp,1);
  850.     l = NoSignExtend(dvifp,1);
  851.     GetBytes(dvifp, n, a+l);
  852.     n[a+l] = '\0';
  853.     if ((tfontptr=(struct font_entry *)malloc(sizeof(struct font_entry))) == NULL)
  854.                Fatal("can't malloc space for font_entry");
  855.     tfontptr->next = hfontptr;
  856.  
  857.     fontptr = hfontptr = tfontptr;
  858.     fontptr->k = k;
  859.     fontptr->c = c;
  860.     fontptr->s = s;
  861.     fontptr->d = d;
  862.     fontptr->a = a;
  863.     fontptr->l = l;
  864.     strcpy(fontptr->n, n);
  865.     fontptr->font_space = s/6;
  866. #ifdef USEGLOBALMAG
  867.     fontptr->font_mag = (int)((ActualFactor((int)(((float)s/(float)d)*1000.0 + 0.5)) *
  868.     ActualFactor(mag) *
  869.     (float)RESOLUTION * 5.0) + 0.5);
  870. #else
  871.     fontptr->font_mag = (int)((ActualFactor((int)(((float)s/(float)d)*1000.0 + 0.5)) *
  872.     (float)RESOLUTION * 5.0) + 0.5);
  873. #endif
  874.     sprintf(fontptr->name, "%s%s.%dpxl", ((a==0)?FONTAREA:""), n, fontptr->font_mag);
  875.     if (fontptr != pfontptr)
  876.     { if (pfontptr != NULL)
  877.                  fclose(pxlfp);
  878. #ifdef Debug
  879.       fprintf(stderr,"Opening PXL file \"%s\"\n", fontptr->name);
  880. #endif
  881.           if ((pxlfp=fopen(fontptr->name,"r")) == NULL)
  882.          Fatal("can't open PXL file \"%s\"", fontptr->name);
  883.           pfontptr = fontptr;
  884.         }
  885.     if ((t=NoSignExtend(pxlfp,4)) != PXLID)
  886.                Fatal("PXL ID = %d, can only process PXL ID = %d files", t, PXLID);
  887.     fseek(pxlfp, 0, 2);
  888.     fseek(pxlfp, -20, 1);
  889.     t = NoSignExtend(pxlfp,4);
  890.     if ((fontptr->c!=0) && (t!=0) && (fontptr->c!=t))
  891.                Warning("font = \"%s\",\n-->font checksum = %d,\n-->dvi checksum = %d",
  892.                        fontptr->name, fontptr->c, t);
  893.     fontptr->magnification = NoSignExtend(pxlfp,4);
  894.     fontptr->designsize = NoSignExtend(pxlfp,4);
  895.     fseek(pxlfp, NoSignExtend(pxlfp,4)*4, 0);
  896.     for (i=FIRSTPXLCHAR; i<=LASTPXLCHAR; i++)
  897.     { tcharptr = &(fontptr->ch[i]);
  898.           tcharptr->wp = NoSignExtend(pxlfp,2);  tcharptr->hp = NoSignExtend(pxlfp,2);
  899.           tcharptr->xoffp = SignExtend(pxlfp,2);  tcharptr->yoffp = SignExtend(pxlfp,2);
  900.           tcharptr->pfrp = NoSignExtend(pxlfp,4);
  901.           tcharptr->tfmw = ((float)NoSignExtend(pxlfp,4)*(float)s) / (float)(1<<20);
  902.           tcharptr->pxlw = PixRound(tcharptr->tfmw, conv);
  903.           if ((tcharptr->hp*((tcharptr->wp+15)/16)) >= BIGBGCHAR)
  904.                   tcharptr->istoobig = TRUE;
  905.           if (i < FIRSTBGCHAR)
  906.           {  tcharptr->bgfont = partfont;
  907.                  tcharptr->bgchar = partchar+i;
  908.               }  else  
  909.           {  tcharptr->bgfont = fullfont;
  910.                  tcharptr->bgchar  = i;
  911.               }
  912.         }
  913.     fullfont = (fullfont+1) % NBGFONTS;
  914.     if (fullfont == 0)  {
  915.                partfont = NBGFONTS;
  916.                partchar = FIRSTBGCHAR;
  917.             }  else  {
  918.                partchar += FIRSTBGCHAR;
  919.                if (partchar > LASTBGCHAR)  {
  920.                   partfont++;
  921.                   partchar = FIRSTBGCHAR;
  922.                }
  923.             }
  924. }
  925.  
  926.  
  927. /**********************************************************************/
  928. /***************************  SkipFontDef  ****************************/
  929. /**********************************************************************/
  930.  
  931. /*  If font definition has already been read in then use this routine to
  932.     skip over the definition bytes in the dvi format file. */
  933.  
  934. static int SkipFontDef (k)
  935.   int k;
  936. {
  937.     int a, l;
  938.     char n[STRSIZE];
  939.     
  940.     NoSignExtend(dvifp,4);
  941.     NoSignExtend(dvifp,4);
  942.     NoSignExtend(dvifp,4);
  943.     a = NoSignExtend(dvifp,1);
  944.     l = NoSignExtend(dvifp,1);
  945.     GetBytes(dvifp, n, a+l);
  946. }
  947.  
  948.  
  949. /**********************************************************************/
  950. /*****************************  AllDone  ******************************/
  951. /**********************************************************************/
  952.  
  953. /*  this routine is used to exit the program. It will reset the terminal
  954.     and if a log file exists then a message to that effect will be
  955.     printed as well. */
  956.  
  957. AllDone()
  958. {
  959.     char t;
  960.  
  961.     ResetTerm();
  962.     printf("\033:0;0m");  /* BBNMOV  Move Graphically */
  963.     if (G_errenc && G_logging == 1)  {
  964.            fseek(G_logfp, 0, 0);
  965.            while ((t=getc(G_logfp)) != EOF)
  966.               putchar(t);
  967.         }
  968.     if (G_logging == 1) printf("Log file created\n");
  969.            exit(G_errenc);
  970. }
  971.  
  972.  
  973.  
  974. /**********************************************************************/
  975. /****************************  ReadInt  *******************************/
  976. /**********************************************************************/
  977.  
  978. /*  this routine is used to read in an integer from the stdin stream.
  979.     This routine is necessary since the terminal is running in
  980.     CBREAK mode and therefore will not do editing of the input
  981.     stream for one.  */
  982.  
  983. ReadInt()
  984. {
  985.    int value = 0;
  986.    int byte;
  987.  
  988.    while ((byte = getchar()) != /* tcb.t_brkc */ 10)
  989.     { if (byte >= 48 /* "0" */ && byte <= 57 /* "9" */)
  990.           value = value * 10 + byte - 48;
  991.       if (byte == tty.sg_erase) value = value / 10;
  992.         }
  993.    return(value);
  994. }
  995.           
  996.  
  997. /**********************************************************************/
  998. /******************************  Fatal  *******************************/
  999. /**********************************************************************/
  1000.  
  1001. /* issue a fatal error message */
  1002.  
  1003. Fatal(fmt, args)
  1004.    char *fmt;   /* format    */
  1005. /*   char *args;  /* arguments */
  1006.  
  1007.  {
  1008. #ifdef Debug
  1009.    fprintf(stderr,"Fatal error---The format is %s\n",fmt); fflush(stderr);
  1010. #endif
  1011.    if (G_logging == 1)
  1012.     { fprintf(G_logfp, "%s: FATAL--", G_progname);
  1013.       _doprnt(fmt, &args, G_logfp);
  1014.       fprintf(G_logfp, "\n");
  1015.     }
  1016.  
  1017.    printf("\033:0;0m");  /* BBNMOV  Move Graphically */
  1018.    ResetTerm();
  1019.    fprintf(stderr, "\n%s: FATAL--", G_progname);
  1020.    _doprnt(fmt, &args, stderr);
  1021.    fprintf(stderr, "\n\n");
  1022.    fflush(stderr);
  1023.    if (G_logging == 1) printf("Log file created\n");
  1024.    exit(1);
  1025. }
  1026.  
  1027.  
  1028. /**********************************************************************/
  1029. /*****************************  GetBytes  *****************************/
  1030. /**********************************************************************/
  1031.  
  1032.    /* get n bytes from file fp */
  1033.  
  1034. GetBytes(fp, cp, n)
  1035.    register FILE *fp;  /* file pointer      */
  1036.    register char *cp;  /* character pointer */
  1037.    register int n;     /* number of bytes   */
  1038.  
  1039. {
  1040.    while (n--)
  1041.       *cp++ = getc(fp);
  1042. }
  1043.  
  1044.  
  1045. /**********************************************************************/
  1046. /***************************  GotInterrupt  ***************************/
  1047. /**********************************************************************/
  1048.  
  1049.    /* don't leave terminal in a weird state */
  1050.  
  1051. void GotInterrupt(sig)
  1052.    int sig;
  1053.  
  1054. {
  1055.    ResetTerm();
  1056.    if (G_logging == 1) printf("Log file created\n");
  1057.    exit(G_errenc);
  1058. }
  1059.  
  1060.  
  1061. /**********************************************************************/
  1062. /*****************************  InitTerm  *****************************/
  1063. /**********************************************************************/
  1064.  
  1065.    /* initialize terminal */
  1066.  
  1067. InitTerm()
  1068.  
  1069. {
  1070.    register int i;  /* loop index */
  1071.  
  1072.    printf("\033:[");   /* BBNPSH  Push Context */
  1073.    printf("\033[?7l");  /* RM DECAWM  no right margin wrap wanted. */
  1074.    printf("\033:2L");  /* BBNSFP  Set Font Parameters to reset all fonts */
  1075.    printf("\033:7o");  /* BBNSTO  Set Text Operation */
  1076.  
  1077.    for (i=(-XDIFFMAX); i<=XDIFFMAX; i++)  /* load horizontal skips */
  1078.       printf("\033P:0;%d;%c;0;0;0;0;%dL",
  1079.              XDIFFORG+i, (char)('0'+NBGFONTS+(NBGFONTS+2)/3), i);  /* BBNLFC  Load Font Character */
  1080.    for (i=(-YDIFFMAX); i<=YDIFFMAX; i++)  /* load vertical skips */
  1081.       printf("\033P:0;%d;%c;0;0;0;0;0;%dL",
  1082.              YDIFFORG+i, (char)('0'+NBGFONTS+(NBGFONTS+2)/3), i);  /* BBNLFC  Load Font Character */
  1083.    printf("\033)%c", (char)('0'+NBGFONTS+(NBGFONTS+2)/3));  /* SCS  Select G1 to be a loadable font */
  1084. }
  1085.  
  1086.  
  1087. /**********************************************************************/
  1088. /*****************************  LoadRast  *****************************/
  1089. /**********************************************************************/
  1090.  
  1091.    /* load raster pattern into BitGraph */
  1092.  
  1093. LoadRast(pxlfp, w, h)
  1094.    FILE *pxlfp;  /* PXL file pointer           */
  1095.    int w;        /* width of raster in pixels  */
  1096.    int h;        /* height of raster in pixels */
  1097.  
  1098. {
  1099.    register int ew;  /* extra word at end of row? */
  1100.    register int t;   /* temporary                 */
  1101.    register int ww;  /* width of raster in words  */
  1102.  
  1103.    ww = (w+15) / 16;
  1104.    ew = ww & 1;
  1105.    while (h--)  {
  1106.       t = ww;
  1107.       while (t--)
  1108.          PutOut(NoSignExtend(pxlfp,2));
  1109.       if (ew)
  1110.          NoSignExtend(pxlfp,2);
  1111.    }
  1112. }
  1113.  
  1114.  
  1115. /**********************************************************************/
  1116. /***************************  NoSignExtend  ***************************/
  1117. /**********************************************************************/
  1118.  
  1119.    /* return n byte quantity from file fd */
  1120.  
  1121. NoSignExtend(fp, n)
  1122.    register FILE *fp;  /* file pointer    */
  1123.    register int n;     /* number of bytes */
  1124.  
  1125. {
  1126.    register int x;   /* number being constructed */
  1127.  
  1128.    x = 0;
  1129.    while (n--)  {
  1130.       x <<= 8;
  1131.       x |= getc(fp);
  1132.    }
  1133.    return(x);
  1134. }
  1135.  
  1136.  
  1137. /**********************************************************************/
  1138. /*****************************  PixRound  *****************************/
  1139. /**********************************************************************/
  1140.  
  1141.    /* return rounded number of pixels */
  1142.  
  1143. PixRound(x, conv)
  1144.    register int x;       /* in DVI units      */
  1145.    register float conv;  /* conversion factor */
  1146.  
  1147. {
  1148.    return((int)((float)x*conv+0.5));
  1149. }
  1150.  
  1151.  
  1152. /**********************************************************************/
  1153. /******************************  PutOut  ******************************/
  1154. /**********************************************************************/
  1155.  
  1156.    /* put a 16-bit raster pattern to the BitGraph */
  1157.  
  1158. PutOut(x)
  1159.    register int x;  /* number to put out */
  1160.  
  1161. {
  1162.    int negative;                      /* was x negative? */
  1163.    register int part1, part2, part3;  /* parts of number */
  1164.  
  1165.    negative = FALSE;
  1166.    if (x < 0)  {
  1167.       negative = TRUE;
  1168.       x = -x;
  1169.    }
  1170.    part1 = (x & 0xfc00) >> 10;
  1171.    part2 = (x & 0x03f0) >>  4;
  1172.    part3 = (x & 0x000f);
  1173.    if (part1)  {
  1174.       putchar(0100+part1);
  1175.       putchar(0100+part2);
  1176.    }  else
  1177.       if (part2)
  1178.          putchar(0100+part2);
  1179.    if (negative)
  1180.       putchar(040+part3);
  1181.    else
  1182.       putchar(060 + part3);
  1183. }
  1184.  
  1185.  
  1186. /**********************************************************************/
  1187. /****************************  ResetTerm  *****************************/
  1188. /**********************************************************************/
  1189.  
  1190.    /* Reset Terminal */
  1191.  
  1192. ResetTerm()
  1193.  
  1194. {
  1195.    printf("\033:2L");  /* BBNSFP  Set Font Parameters to reset all fonts */
  1196.    printf("\033:]");  /* Pop Context (BBNPOP) */
  1197.    fflush(stdout);
  1198.    if (G_interactive)
  1199.       ioctl(0, TIOCSETP, &G_intty);  /* restore modes */
  1200. }
  1201.  
  1202.  
  1203. /**********************************************************************/
  1204. /****************************  RulePixels  ****************************/
  1205. /**********************************************************************/
  1206.  
  1207.    /* return number of pixels in a rule */
  1208.  
  1209. RulePixels(x, conv)
  1210.    register int x;       /* in DVI units      */
  1211.    register float conv;  /* conversion factor */
  1212.  
  1213. {
  1214.    register int n;
  1215.  
  1216.    n = x*conv;
  1217.    if (n < x*conv)
  1218.       return(n+1);
  1219.    else
  1220.       return(n);
  1221. }
  1222.  
  1223.  
  1224. /**********************************************************************/
  1225. /****************************  SignExtend  ****************************/
  1226. /**********************************************************************/
  1227.  
  1228.    /* return n byte quantity from file fd */
  1229.  
  1230. SignExtend(fp, n)
  1231.    register FILE *fp;  /* file pointer    */
  1232.    register int n;     /* number of bytes */
  1233.  
  1234. {
  1235.    int n1;           /* number of bytes          */
  1236.    register int x;   /* number being constructed */
  1237.  
  1238.    x = 0;
  1239.    n1 = n;
  1240.    while (n--)  {
  1241.       x <<= 8;
  1242.       x |= getc(fp);
  1243.    }
  1244.    x<<=32-8*n1;  x>>=32-8*n1;  /* sign extend */
  1245.    return(x);
  1246. }
  1247.  
  1248.  
  1249. /**********************************************************************/
  1250. /*****************************  Warning  ******************************/
  1251. /**********************************************************************/
  1252.  
  1253.    /* issue a warning */
  1254.  
  1255. Warning(fmt, args)
  1256.    char *fmt;   /* format    */
  1257.    char *args;  /* arguments */
  1258. {
  1259.    if (G_logging = 0) 
  1260.     {G_logfp=fopen(G_Logname,"w+");
  1261.      G_logging = 1;
  1262.      if (G_logfp == NULL) G_logging = -1;
  1263.     }
  1264.  
  1265.    G_errenc = TRUE;
  1266.    if (G_logging == 1)
  1267.     { _doprnt(fmt, &args, G_logfp);
  1268.        fprintf(G_logfp, "\n");
  1269.     }
  1270. }
  1271.  
  1272. /**********************************************************************/
  1273. /**************************  ActualFactor  ****************************/
  1274. /**********************************************************************/
  1275.  
  1276.    /* compute the actual size factor given the approximation */
  1277.  
  1278. float ActualFactor(unmodsize)
  1279.     int unmodsize;    /* actually factor * 1000 */
  1280. {
  1281.     float realsize;    /* the actual magnification factor */
  1282.     
  1283.     realsize = (float)unmodsize / 1000.0;
  1284.     /* a real hack to correct for rounding in some cases--rkf */
  1285.     if(unmodsize==1095) realsize = 1.095445;  /*stephalf*/
  1286.     else if(unmodsize==1315) realsize=1.314534;  /*stepihalf*/
  1287.     else if(unmodsize==2074) realsize=2.0736;  /*stepiv*/
  1288.     else if(unmodsize==2488) realsize=2.48832;  /*stepv*/
  1289.     else if(unmodsize==2986) realsize=2.985984;  /*stepiv*/
  1290.     /* the remaining magnification steps are represented with sufficient
  1291.        accuracy already */
  1292.     return(realsize);
  1293. }
  1294.